<?php
namespace PsiFramework\Applications\DefaultApp\Classes\DAO;

import("Classes.DAO.AbstractDAO");
import("Applications.DefaultApp.Classes.Model.Page");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Applications\DefaultApp\Classes\Model\Page;


/**
 * AlloMedia
 * Portail des professionnels
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The StyleDAO is the Data Access Object for the class Style
 *
 * @package DAO
 */
class PageDAO extends AbstractDAO
{

    protected static $_singleton = null;

    public function retrieveByPartialName(
        /*string*/ $partialName,
        /*string*/ $language=null,
        /*bool*/   $exactName=false
    )
    {

        $colNom = new SqlColumn("page_name");
        $colTitle = new SqlColumn("page_title");

        $value = mb_strtoupper($partialName, 'utf-8');
        if (!$exactName) {
        	$value = '%' . $value . "%" ;
        }

        $sqlValue = new SqlValue($value);

        $upper = new SqlFunction("UPPER");
        $upper->addParameter($colNom);

        $upperTwo = new SqlFunction("UPPER");
        $upperTwo->addParameter($colTitle);

        $op = null ;
        $opTwo = null ;
        if ($exactName) {
            $op = new SqlOperation(SqlOperation::$equal);
            $opTwo = new SqlOperation(SqlOperation::$equal);
        } else {
            $op = new SqlOperation(SqlOperation::$like);
            $opTwo = new SqlOperation(SqlOperation::$equal);
        }

        $op->addParameter($upper);
        $op->addParameter($sqlValue);

        $opTwo->addParameter($upperTwo);
        $opTwo->addParameter($sqlValue);

        $or = new SqlOperation(SqlOperation::$or);
        $or->addParameter($op);
        $or->addParameter($opTwo);

        $and = new SqlOperation(SqlOperation::$and);
        $and->addParameter($or);

        if (!isNullOrEmptyString($language)) {
            $langOp = new SqlOperation(SqlOperation::$equal);

            $colLang = new SqlColumn("page_language");
            $langUpper = new SqlFunction("UPPER");
            $langUpper->addParameter($colLang);

            $langOp->addParameter($langUpper);
            $langOp->addParameter(
                new SqlValue(mb_strtoupper($language, 'utf-8'))
            );

            $and->addParameter($langOp);
        }

        $result = $this->retrieveAll(new QueryFilter($and));

        if (!$exactName) {
        	return $result ;
        } else if (count($result)==1) {
        	return $result[0] ;
        } else {
        	return null ;
        }

    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return PageDAO    instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new PageDAO($objectParameters);
        }

        return self::$_singleton;
    }

}
